﻿using System;
using System.Data;
using Besture.Bll;
using Besture.Common;

namespace Besture.UI.Admin.Master
{
    public partial class Admin : System.Web.UI.MasterPage
    {
        public int iDdata = 0;
        public string _Title { get; set; }

        /// <summary>
        /// 当前模块ID
        /// </summary>
        public string curModulesId = string.Empty;
        /// <summary>
        /// 当前模块名
        /// </summary>
        public string curModulesName = string.Empty;
        /// <summary>
        /// 当前页面ID
        /// </summary>
        public string curPageId = string.Empty;
        /// <summary>
        /// 当前父模块Url
        /// </summary>
        public string pageUrl = string.Empty;
        /// <summary>
        /// 当前页面名称
        /// </summary>
        public string curPageName = string.Empty;

        AdminPagesDac adminPagesDac = new AdminPagesDac();

        protected void Page_Load(object sender, EventArgs e)
        {
            GetOrderNoHtml();

            //临时写法
            DataTable tableModules = new DataTable();

            string partid = StringHelper.Query("partid");
            string curUrl = "";
            //if (string.IsNullOrEmpty(partid))
            //{
            //    if (!string.IsNullOrEmpty(StringHelper.Query("type")))
            //    {
            //        curUrl = Request.Url.AbsolutePath.ToLower() + "?type=" + StringHelper.Query("type");
            //    }
            //    else
            //    {
            //        curUrl = Request.Url.AbsolutePath.ToLower();
            //    }
            //}
            //else
            //{
            curUrl = Request.Url.AbsolutePath.ToLower();
            if (!string.IsNullOrEmpty(partid))
            {
                curUrl = Request.Url.PathAndQuery.ToLower();
                tableModules = adminPagesDac.GetDataTableByUrl(curUrl);
                if (tableModules.Rows.Count == 0)
                {
                    curUrl = Request.Url.AbsolutePath.ToLower() + "?partid=" + partid;
                }
            }
            //}

            curUrl = curUrl.Substring(curUrl.IndexOf("/admin"));
            if (curUrl.IndexOf("key=") == -1 && curUrl.IndexOf("longcontent") == -1 && curUrl.ToLower().IndexOf("aboutcdpset") == -1 && curUrl.ToLower().IndexOf("jobstemplateset") == -1 && partid != "72" && partid != "73" && curUrl.ToLower().IndexOf("twosolutionsset") == -1 && curUrl.ToLower().IndexOf("twocategoryset") == -1)
            {
                tableModules = adminPagesDac.GetDataTableByUrl(curUrl);
                if (tableModules.Rows.Count == 0)
                {
                    curUrl = curUrl.Replace("set.aspx", "list.aspx");
                }
            }

            //if (curUrl.IndexOf("partcontentlist.aspx") > -1)
            //{
            //    curUrl = Request.Url.PathAndQuery.ToLower().ToString();
            //}

            tableModules = adminPagesDac.GetDataTableByUrl(curUrl);
            if (tableModules.Rows.Count == 0)
            {
                curUrl = Request.Url.PathAndQuery.ToLower();
                curUrl = curUrl.Substring(curUrl.IndexOf("/admin"));
                if (curUrl.IndexOf("key=") == -1 && curUrl.IndexOf("longcontent") == -1 && curUrl.ToLower().IndexOf("aboutcdpset") == -1 && curUrl.ToLower().IndexOf("jobstemplateset") == -1 && partid != "72" && partid != "73" && curUrl.ToLower().IndexOf("twosolutionsset") == -1 && curUrl.ToLower().IndexOf("twocategoryset") == -1)
                {
                    curUrl = curUrl.Replace("set.aspx", "list.aspx");
                }
                tableModules = adminPagesDac.GetDataTableByUrl(curUrl);
            }
            if (tableModules.Rows.Count > 0)
            {
                //临时写法
                curModulesId = tableModules.Rows[0]["ModulesID"].ToString();
                curModulesName = tableModules.Rows[0]["ModulesName"].ToString();
                curPageId = tableModules.Rows[0]["id"].ToString();
                curPageName = tableModules.Rows[0]["Name"].ToString();

            }
            BindData();

        }


        public string GetOrderNoHtml()
        {
            string sHtml = string.Empty;
            var vDt = QueryDac.GetTable("SELECT top 1 * FROM TBOrder WHERE OrderStatus=1 ORDER BY CreateTime");
            if (vDt != null && vDt.Rows.Count > 0)
            {
                iDdata = vDt.Rows.Count;
                sHtml =
                    string.Format(
                        "有 <a  style='color: #0541FF;' href='/admin/Order/OrderSet.aspx?id={0}' onclick='start()'> {1} </a> 个新订单,请及时确定",
                        Convert.ToString(vDt.Rows[0]["ID"]), vDt.Rows.Count);
            }
            return sHtml;
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        public void BindData()
        {
            if (AdminUserService.AdminLoginUser==null)
            {
                return;
            }
            rpModules.DataSource = AdminUserService.AdminLoginUser.tableModulesPermissions;
            rpModules.DataBind();
            if (!string.IsNullOrEmpty(curModulesId))
            {
                //DataTable tablePages =  adminPagesDac.GetDataTableByModulesId(curModulesId);
                rpPages.DataSource = AdminUserService.GetPageTableByModulesId(curModulesId);
                rpPages.DataBind();
            }
        }

        protected void lbUpdatePassword_Click(object sender, EventArgs e)
        {

        }

        protected void lbOut_Click(object sender, EventArgs e)
        {

        }



        /// <summary>
        /// 判断是否当前模块
        /// </summary>
        /// <param name="modulesID"></param>
        /// <returns></returns>
        public string IsCurModules(string modulesID)
        {
            if (string.IsNullOrEmpty(curModulesId))
            {
                //if (Session["curModulesId"] != null)
                //{
                if (curModulesId.Equals(modulesID))
                {
                    return "class='on'";
                }
                //}
            }
            else if (curModulesId.Equals(modulesID))
            {
                return "class='on'";
            }
            return "";
        }

        /// <summary>
        /// 判断是否当前页面
        /// </summary>
        /// <param name="pageID"></param>
        /// <returns></returns>
        public string IsCurPages(string pageID)
        {
            if (string.IsNullOrEmpty(pageID))
            {
                //if (Session["curPageId"] != null)
                //{
                if (curPageId.Equals(curPageId))
                {
                    return "class='on'";
                }
                //}
                return "";
            }
            else if (curPageId.Equals(pageID))
            {
                return "class='on'";
            }
            return "";
        }
    }
}